package com.myk.game.gobangchess.rooms.absrooms;


import com.myk.game.gobangchess.DataManager;
import com.myk.game.gobangchess.Player;
import com.myk.game.gobangchess.constants.Msger;

/**
 * 多视角象棋类抽象房间
 */
public abstract class AbsDiffViewChessRoom extends AbsChessRoom {

    public AbsDiffViewChessRoom(String roomName, String roomIntroduce, String chessName, int[][] curGrids, boolean isSupportPersonal, boolean isSaveHistoryToFile,String ... modes) {
        super(roomName, roomIntroduce, chessName, curGrids, isSupportPersonal, isSaveHistoryToFile,modes);
    }

    @Override
    public final String getGameInfo() {
        return super.getGameInfo();
    }

    /**
     * 0：观众视角，  1：黑方视角， 2：白方视角， 3：终局全开放视角
     */
    protected String getGameInfo(int viewPoint) {
        return getGameInfo(); //默认行为但是一般都会重写的
    }

    @Override
    protected void noticeAllRefreshGameInfo() {
        if (cur == 0) { //终局时，所有人都能看到双方棋子的全部信息
            String compGameInfo = getGameInfo(3);
            if (player_black != player_white){
                if (player_black != null) {
                    player_black.sendMessage(Msger.refreshGameInfo, compGameInfo);
                }
                if (player_white != null) {
                    player_white.sendMessage(Msger.refreshGameInfo, compGameInfo);
                }
            }
            else {
                if (player_black != null){
                    player_black.sendMessage(Msger.refreshGameInfo, compGameInfo);
                }
            }
            for (Player player : spectatorList) {
                if (player != null) {
                    player.sendMessage(Msger.refreshGameInfo, compGameInfo);
                }
            }
        }
        else { //正常对局时
            if (player_black != player_white){
                if (player_black != null) {
                    String blackGameInfo = getGameInfo(1);
                    player_black.sendMessage(Msger.refreshGameInfo, blackGameInfo);
                }
                if (player_white != null) {
                    String whiteGameInfo = getGameInfo(2);
                    player_white.sendMessage(Msger.refreshGameInfo, whiteGameInfo);
                }
            }
            else {
                if (player_black != null){
                    String gameInfo = getGameInfo(cur);
                    player_black.sendMessage(Msger.refreshGameInfo, gameInfo);
                }
            }
            if (spectatorList.size() > 0) {
                String specGameInfo = getGameInfo(0);
                for (Player player : spectatorList) {
                    if (player != null) {
                        player.sendMessage(Msger.refreshGameInfo, specGameInfo);
                    }
                }
            }
        }
    }

    @Override
    public void playerEnter(Player player) {
        removeSpectator(player);//先移出，避免因奇怪原因玩家多次进入，出现多个观众的问题
        addSpectator(player);
        player.setEnteringRoom(this);
        player.sendMessage(Msger.enterRoomSuccess, "Ok");
        player.sendMessage(Msger.refreshGameInfo, getGameInfo(0)); //刚进来只可能是观众，更新一下观众视角的游戏数据
        noticeAllRefreshRoomInfo();
        DataManager.noticeAllRefreshRoomList();
        noticePlayerCurrentCountDown(player); //通知刚进来的用户显示当前的倒计时动画
        System.out.println(player.getNickNameOrTemp() + "进入" + getRoomName());
    }

    @Override
    public void playerReConnectEnter(Player player) {
        player.sendMessage(Msger.enterRoomSuccess, "Ok"); //通知你进入房间成功
        player.sendMessage(Msger.refreshPlayerInfo, player.getPlayerInfo()); //更新角色
        player.sendMessage(Msger.refreshRoomInfo, getRoomInfo()); //更新一下游戏数据
        System.out.println(player.getNickNameOrTemp() + "重连到" + getRoomName());
        boolean isPlayingStarted = player_black != null && player_white != null; //黑白位置都有玩家，说明对弈是开始的状态
        boolean isOnSeatPlayer = player == player_black || player == player_white; //重连的玩家属于在座玩家
        if (isPlayingStarted && isOnSeatPlayer) { //checkGameStart()中满足要求了会刷新界面，用这种方式防止重复刷新
            if (overFlag == 2 || overFlag == 3 || overFlag == 4) { //对弈结束状态成为棋手时，分发继续按钮
                overFlag = 2; //重进双方重新确认判断
                dispatchContinueButton();
            }
            noticeOnSeatReConnectMove(player);
        }
        else {
            if (cur == 0){
                String compGameInfo = getGameInfo(3);
                player.sendMessage(Msger.refreshGameInfo, compGameInfo);
            }
            else {
                if (player_black == player){
                    String blackGameInfo = getGameInfo(1);
                    player.sendMessage(Msger.refreshGameInfo, blackGameInfo);
                }
                else if (player_white == player){
                    String whiteGameInfo = getGameInfo(2);
                    player.sendMessage(Msger.refreshGameInfo, whiteGameInfo);
                }
                else{
                    String specGameInfo = getGameInfo(0);
                    player.sendMessage(Msger.refreshGameInfo, specGameInfo);
                }
            }
        }
    }

    @Override
    public void noticeOnSeatReConnectMove(Player player) {
        if (cur == 1) {
            player.sendMessage(Msger.youCanMove, "Ok");
        }
        else if (cur == 2) {
            player.sendMessage(Msger.youCanMove, "Ok");
        }
        else if (cur == 3) {
            player.sendMessage(Msger.youCanMove, "Ok");
        }
        else {
            player.sendMessage(Msger.youCanMove, "Ok");
        }

        if (cur == 0){
            String compGameInfo = getGameInfo(3);
            player.sendMessage(Msger.refreshGameInfo, compGameInfo);
        }
        else {
            if (player_black == player_white && player_black == player){
                String compGameInfo = getGameInfo(3);
                player.sendMessage(Msger.refreshGameInfo, compGameInfo);
            }
            else if (player_black == player) {
                String blackGameInfo = getGameInfo(1);
                player.sendMessage(Msger.refreshGameInfo, blackGameInfo);
            }
            else if (player_white == player) {
                String whiteGameInfo = getGameInfo(2);
                player.sendMessage(Msger.refreshGameInfo, whiteGameInfo);
            }
            else {
                String specGameInfo = getGameInfo(0);
                player.sendMessage(Msger.refreshGameInfo, specGameInfo);
            }
        }

        player.dispatchCustomBottom();
        noticePlayerCurrentCountDown(player);
    }
}
